<?php
namespace backend\components;

use Yii;
use yii\helpers\ArrayHelper;
use backend\components\Utools;

final class Chat {
    

    public $token = '';

    public function __construct()
    {
        $session = Yii::$app->session;
        //记录token到session中，并对其时间加以判断
        
        if(!isset($session['token']) || $session['token']['end_time'] < time())
        {
            $wm_token = $this->get_wm_token();
            $data = [
                'value' => $wm_token->access_token,
                'end_time' => time()+3000000,
            ];
            $session['token'] = $data;
            
        }
        $this->token = $session['token']['value'];   
    }

    /**
     * 获取环信token
     * @param url 接口地址
     * @param data 传递参数
     * @return responser 返回信息
     */
    protected function get_wm_token()
    {
        
        $url = 'https://a1.easemob.com/1165180419146683/jnwmpaidan/token';
        //$data = json_encode($data);
        $headers = array(  
            "Content-Type: application/json; charset=utf-8",  
        );
        $data = array(
            "grant_type"=> "client_credentials",
            "client_id"=>"YXA6CLPhkEO4EeiaxXUZKb0cpw",
            "client_secret"=>"YXA6fws1EKPP2BTtk6HlcVaoBegCosY",
        );
        $data = json_encode($data);
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
           echo 'Error'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        $tmpInfo = json_decode($tmpInfo);
        return $tmpInfo; // 返回数据
    }

    /**
     * 供环信使用的 vpost
     * @param url 接口地址
     * @param data 传递参数 一维数组 不需要转json
     * @return responser 返回信息
     */
    public function vpost($url,$data='')
    {
        $headers = array(  
            "Content-Type: application/json; charset=utf-8",  
            "Authorization: Bearer ".$this->token,
        );
        $data = json_encode($data);
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
           echo 'Error'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        $tmpInfo = json_decode($tmpInfo);
        return $tmpInfo; // 返回数据
    }

    /**
     * 供环信使用的 vget
     * @param url 接口地址
     * @return responser 返回信息
     */
    public function vget($url)
    {
        $headers = array(  
            "Content-Type: application/json; charset=utf-8",  
            "Authorization: Bearer ".$this->token,
        );
        $curl = curl_init(); // 启动一个CURL会话
        //设置选项，包括URL
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        //执行并获取HTML文档内容

        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error'.curl_error($curl);//捕抓异常
        }
        //释放curl句柄
        curl_close($curl);
        $output = json_decode($output);
        return $output;
    }
	
}